package com.hqc.gallery.web.control;

import java.net.URL;

import com.google.gdata.client.photos.PicasawebService;
import com.google.gdata.data.PlainTextConstruct;
import com.google.gdata.data.photos.AlbumEntry;
import com.google.gdata.data.photos.UserFeed;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class ProcessAlbumAction extends ActionSupport {
	
	public String insert() {
		try {
			PicasawebService myService = new PicasawebService("exampleCo-exampleApp-1");
			myService.setUserCredentials("donghaiit@gmail.com", "Ocean1980");
			URL feedUrl = new URL("https://picasaweb.google.com/data/feed/api/user/donghaiit?kind=album");
			String name = ((String[])ActionContext.getContext().getParameters().get("name"))[0];
			String description = ((String[])ActionContext.getContext().getParameters().get("description"))[0];
			AlbumEntry myAlbum = new AlbumEntry();

			myAlbum.setTitle(new PlainTextConstruct(name));
			myAlbum.setDescription(new PlainTextConstruct(description));

			myService.insert(feedUrl, myAlbum);
		} catch (Exception ex) {
			ex.printStackTrace();
			return INPUT;
		}
		return SUCCESS;
	}
	
	public String delete() {
		try {
			PicasawebService myService = new PicasawebService("exampleCo-exampleApp-1");
			myService.setUserCredentials("donghaiit@gmail.com", "Ocean1980");
			URL feedUrl = new URL("https://picasaweb.google.com/data/feed/api/user/donghaiit?kind=album");
			UserFeed myUserFeed = myService.getFeed(feedUrl, UserFeed.class);
			String id = ((String[])ActionContext.getContext().getParameters().get("albumId"))[0];
			for (AlbumEntry myAlbum : myUserFeed.getAlbumEntries()) {
			    if(myAlbum.getId().contains(id)) {
			    	myAlbum.delete();
			    	break;
			    }
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			return INPUT;
		}
		return SUCCESS;
	}
	
}
